﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;
using System.Reflection;
using FastReflectionLib;
using Summer.Exception;
using Summer.Web.Support;
using Summer.Core.Ioc;

#region License
/*
 *  Copyright (c) Dumuzi.cn 2009 , All rights reserved.
 *  CLR Version: 2.0.50727.4927
 *  NameSpace: Summer.Web.Mvc
 *  Author:Edwin Tai,戴佳顺
 *  Msn:edwin19861218@hotmail.com
 *  QQ: 1961218
 *  Date:2009-11-19 15:55:08
 *  Licensed under the Microsoft Public License (Ms-PL)
 *  This license governs use of the accompanying software. 
 *  If you use the software, you accept this license.
 *  If you do not accept the license, do not use the software.
 */
#endregion

namespace Summer.Web.Mvc
{
    public class AbstractMvcHandler : System.Web.Mvc.MvcHandler
    {
        private ControllerBuilder _controllerBuilder;
        public AbstractMvcHandler(RequestContext requestContext)
            : base(requestContext)
        {

        }
        /// <summary>
        // Controller builder to get factory
        /// </summary>
        internal ControllerBuilder ControllerBuilder
        {
            get
            {
                if (_controllerBuilder == null)
                {
                    _controllerBuilder = ControllerBuilder.Current;
                }
                return _controllerBuilder;
            }
            set
            {
                _controllerBuilder = value;
            }
        }
        /// <summary>
        /// Create new instance of control and execute it
        /// </summary>
        /// <param name="httpContext"></param>
        protected override void ProcessRequest(HttpContextBase httpContext)
        {
            AddVersionHeader(httpContext);
            // Get the controller type
            string controllerName = RequestContext.RouteData.GetRequiredString("controller");
            IControllerFactory factory = ControllerBuilder.GetControllerFactory();
            IController controller = null;
            if (controller == null)
            {
                throw new MvcException("The controller created is null");
            }
            try
            {
                controller.Execute(RequestContext);
            }
            finally
            {
                factory.ReleaseController(controller);
            }
        }
        /// <summary>
        /// Create new instance of controller
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="controllerName"></param>
        /// <param name="controller"></param>
        protected virtual void CreateNewInstance(IControllerFactory factory, string controllerName, out IController controller)
        {
            // Instantiate the controller
            try
            {
                controller = factory.CreateController(RequestContext, controllerName);
            }
            catch
            {
                throw new MvcException("Create controller error");
            }
        }
    }
}
